/**
 * 新增账单 数据初始化
 * @param shopId 商铺id
 * @param orderNo 订单号
 * @return {JSON} 返回已成功初始化的数据
*/
export async function initData(shopId,orderNo){
	if (!shopId) {
		return {code:404,error:"参数不全",
			shopId
		}
	}
	const db = uniCloud.database()
	let result ;
	try{
		let shopData = await db.collection("shop").doc(shopId).field("_id,title,logo").get({getOne:true})
		shopData = shopData.result.data
		if (orderNo){
			result = {
				shop_id:shopData._id,
				price:0,
				type:1,
				order_no:"",
				text:"订单收入",
				shop:{
					_id:shopData._id,
					logo:shopData.logo.url,
					title:shopData.title
				},
				user:{},
				order:{},
				create_date:Date.now()
			}
		}else {
			result = {
				shop_id:shopData._id,
				price:0,
				type:1,
				order_no:"",
				text:"订单收入",
				shop:{
					_id:shopData._id,
					logo:shopData.logo.url,
					title:shopData.title
				},
				user:{},
				order:{},
				create_date:Date.now()
			}
		}
		return {
			code:200,
			result
		}
	}catch(error){
		return {
			code:501,
			error
		}
	}
}
/**
 * 订单号识别
 * @param orderNo 订单号
 * @return {JSON} 返回已成功初始化的数据
*/
export async function identifyOrderNo(orderNo,recursion=0,type=0){
	if (!orderNo) {
		return {code:404,error:"参数不全",
			orderNo
		}
	}
	const db = uniCloud.database()
	let result ;
	try{
		let shopBill = await db.collection("shop_bill").where({order_no:orderNo}).field("_id").get({getOne:true})
		if (shopBill.result.affectedDocs && !recursion){
			return {
				code:202,
				error:"当前订单已有记录"
			}
		}
		let TEXT , MONEY ,CreateDate;
		let order = db.collection('orders').field("_id,status,delivery_price,advance_income_price,pay_date,order_no,address,shop_id").getTemp({getOne: true})
		let shop = db.collection('shop').field("_id,title,tel,logo").getTemp({getOne: true})
		let orderData = await db.collection(order,shop).where({order_no:orderNo}).get({getOne:true})
		orderData = orderData.result.data
		if (orderData) {
			if (type===1) {
				TEXT = "商家自配送"
				MONEY = orderData.delivery_price/100
				CreateDate = Date.now()
			}else if (type===0){
				TEXT = "订单收入"
				MONEY = orderData.advance_income_price/100
				CreateDate = orderData.status.create_time
			}
			result = {
				code:200,
				result:{
					shop_id:orderData.shop_id[0]._id,
					price:MONEY,
					type:1,
					order_no:orderData.order_no,
					text:TEXT,
					shop:{
						_id:orderData.shop_id[0]._id,
						logo:orderData.shop_id[0].logo.url,
						title:orderData.shop_id[0].title
					},
					user:{
						_id:orderData.address?orderData.address.user_id:"",
						avatar:orderData.address?orderData.address.avatar:"https://mp-60d04191-dfc8-47dc-b1e1-36d6fc279352.cdn.bspapp.com/headimage/2a9e44a0669a0e6c3d44e12ae2dd8c24.jpg",
						nickname:orderData.address?orderData.address.name:"未知用户"
					},
					order:{
						order_no:orderData.order_no,
						total_fee:MONEY,
						delivery_fee:orderData.delivery_price
					},
					create_date:CreateDate
				}
			}
		}
		else {
			result = {
				code:422,
				error:"当前订单号未匹配到订单"
			}
		}
		
		return result
	}catch(error){
		return {
			code:501,
			error
		}
	}
}

/**
 * 金额补回数据加载
 * @param shopId 商家id
 * @return {JSON} 返回已成功初始化的数据
*/
export async function amountreplenishment(shopId){
	if (!shopId) {
		return {code:404,error:"参数不全",
			shopId
		}
	}
	const db = uniCloud.database()
	let result ;
	try{
		let TEXT = "金额补回"
		let MONEY = 0
		let shop = await db.collection('shop').doc(shopId).field("_id,title,tel,logo").get({getOne: true})
		shop = shop.result.data
		result = {
			code:200,
			result:{
				shop_id:shop._id,
				price:MONEY,
				type:1,
				order_no: generateOrderNumber('D'),
				text:TEXT,
				shop:{
					_id:shop._id,
					logo:shop.logo.url,
					title:shop.title
				},
				user:{
					avatar:"https://mp-60d04191-dfc8-47dc-b1e1-36d6fc279352.cdn.bspapp.com/icons/shop_logo.png",
					nickname:"易派商家"
				},
				order:{
					order_no:TEXT,
					total_fee: MONEY,
					delivery_fee:0
				},
				create_date:Date.now()
			}
		}
		return result
	}catch(error){
		return {
			code:501,
			error
		}
	}
}

function generateOrderNumber(header='Y') {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getSeconds()).padStart(2, 'v');
    const day = String(now.getDate()).padStart(2, 'i');
    const hours = String(now.getHours()).padStart(2, 'l');
    const minutes = String(now.getMinutes()).padStart(2, 'l');
    const seconds = String(now.getSeconds()).padStart(2, 'l');
    const randomNumber = Math.floor(Math.random() * 10000).toString().padStart(4, '0');

    return `${header}${year}${month}${day}${hours}${minutes}${seconds}${randomNumber}`;
}